import os

import django
from django.db.models import Count

if __name__ == '__main__':
    # django代码的运行要在一个特定的环境中的
    # django flask 本质上都是提供了一个符合WSGI规范的函数
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blogproject.settings')
    django.setup()
    from blog.models import Category

    categories = Category.objects.annotate(
        num_articles=Count('article')).filter(num_articles__gt=0)
    print(categories)
    for  category in categories:
        print(category, category.num_articles)
